// Package chat @program: gateway
//@author: edte
//@create: 2021-12-29 13:24
package chat

import (
	"context"
	"gateway/client"
	"gateway/config/chat"
	"gateway/tools/security"
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc"
	"log"
	"net/http"
	"time"
)

var conn, err = grpc.Dial(*client.ChatAddr, grpc.WithInsecure())
var c = chat.NewProdChatSystemServiceClient(conn)

// CreateChatRoom /*func GetLink() (chat.ProdChatSystemServiceClient, error) {
func CreateChatRoom(token string, roomId int, isPrvate bool) (gin.H, error) {
	if err != nil {
		log.Println(err)
	}

	userinfo, err := security.VerifyAction(token)

	if err != nil {
		return gin.H{"status": http.StatusBadGateway, "message": err.Error()}, err
	}
	_, ok := security.UserMap[userinfo.UserID]
	if !ok {
		return gin.H{"status": http.StatusBadGateway, "message": "账号未登录"}, err
	}
	/*c, err := GetLink()
	if err != nil || c == nil {
		return gin.H{"status": http.StatusBadRequest}, err
	}*/
	cxt, cancel := context.WithTimeout(context.Background(), time.Minute)
	defer cancel()
	form, err := c.GetUserCreateChatRoomForm(cxt, &chat.CreateChatRoomRequest{RoomId: int32(roomId), UserId: int32(userinfo.UserID), IsPrivate: isPrvate})
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": err.Error()}, err
	}
	return gin.H{"status": form.Status, "message": "创建成功"}, err
}
func JoinChatRoom(token string, roomId int) (gin.H, error) {
	userinfo, err := security.VerifyAction(token)
	if err != nil {
		return gin.H{"status": http.StatusBadGateway, "message": err.Error()}, err
	}
	_, ok := security.UserMap[userinfo.UserID]
	if !ok {
		return gin.H{"status": http.StatusBadGateway, "message": "账号未登录"}, err
	}
	/*c, err := GetLink()
	if err != nil {
		return gin.H{"status": http.StatusBadRequest}, err
	}*/
	cxt, cancel := context.WithTimeout(context.Background(), time.Minute)
	defer cancel()
	form, err := c.GetUserJoinChatRoomForm(cxt, &chat.JoinChatRoomRequest{RoomId: int32(roomId), UserId: int32(userinfo.UserID)})
	if err != nil {
		return gin.H{"status": http.StatusBadGateway, "message": err.Error()}, err
	}
	return gin.H{"status": form.Status, "message": "加入成功"}, err
}
func MasterAddUser(token string, roomId int, userId int) (gin.H, error) {
	userinfo, err := security.VerifyAction(token)
	if err != nil {
		return gin.H{"status": http.StatusBadGateway, "message": err.Error()}, err
	}
	_, ok := security.UserMap[userinfo.UserID]
	if !ok {
		return gin.H{"status": http.StatusBadGateway, "message": "账号未登录"}, err
	}
	/*c, err := GetLink()
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": err.Error()}, err
	}*/
	cxt, cancel := context.WithTimeout(context.Background(), time.Minute)
	defer cancel()
	form, err := c.GetUserChatRoomMasterAddUserForm(cxt, &chat.ChatRoomMasterAddUserRequest{RoomId: int32(roomId), UserId: int32(userId), MasterId: int32(userinfo.UserID)})
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": err.Error()}, err
	}
	return gin.H{"status": form.Status, "message": "添加成功"}, err
}
func QuitRoom(token string, roomId int) (gin.H, error) {
	userinfo, err := security.VerifyAction(token)
	if err != nil {
		return gin.H{"status": http.StatusBadGateway, "message": err.Error()}, err
	}
	_, ok := security.UserMap[userinfo.UserID]
	if !ok {
		return gin.H{"status": http.StatusBadGateway, "message": "账号未登录"}, err
	}
	/*c, err := GetLink()
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": err.Error()}, err
	}*/
	cxt, cancel := context.WithTimeout(context.Background(), time.Minute)
	defer cancel()
	form, err := c.GetUserDisBandChatRoomForm(cxt, &chat.DisbandChatRoomsRequest{RoomId: int32(roomId), UserId: int32(userinfo.UserID)})

	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": err.Error()}, err
	}
	return gin.H{"status": form.Status, "message": "添加成功"}, err
}
func QueryMessage(token string, roomId int) (gin.H, error) {
	userinfo, err := security.VerifyAction(token)
	if err != nil {
		return gin.H{"status": http.StatusBadGateway, "message": err.Error()}, err
	}
	_, ok := security.UserMap[userinfo.UserID]
	if !ok {
		return gin.H{"status": http.StatusBadGateway, "message": "账号未登录"}, err
	}
	/*c, err := GetLink()
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": err.Error()}, err
	}*/
	/*cxt, cancel := context.WithTimeout(context.Background(), time.Minute)
	defer cancel()*/
	form, err := c.GetUserChatRoomMessageForm(context.Background(), &chat.QueryUserChatRoomRequest{RoomId: int32(roomId), UserId: int32(userinfo.UserID)})
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": err.Error()}, err
	}
	return gin.H{"status": form.Status, "message": "查询成功", "data": form.Messages}, err
}
func SentMessage(token string, roomId int, message string) (gin.H, error) {
	userinfo, err := security.VerifyAction(token)
	if err != nil {
		return gin.H{"status": http.StatusBadGateway, "message": err.Error()}, err
	}
	_, ok := security.UserMap[userinfo.UserID]
	if !ok {
		return gin.H{"status": http.StatusBadGateway, "message": "账号未登录"}, err
	}
	/*c, err := GetLink()
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": err.Error()}, err
	}*/
	cxt, cancel := context.WithTimeout(context.Background(), time.Minute)
	defer cancel()
	form, err := c.GetUserSentMessageForm(cxt, &chat.SentMessageRequest{RoomId: int32(roomId), UserId: int32(userinfo.UserID), Message: message, Nickname: userinfo.FullName})
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": err.Error()}, err
	}
	return gin.H{"status": form.Status, "message": "发送成功", "data": form.Message}, err
}
